/**
 * 上传文件
 * @returns
 */
const upLoadfile = (file, filename) => {
  // OSS 参数
  let ossConf = {
    reqAddr: 'https://elephant-ad.oss-cn-shenzhen.aliyuncs.com',
    OSSAccessKeyId: 'LTAI4FgnUUpQTrYyWbKHSww2',
    policy: 'eyJleHBpcmF0aW9uIjoiMjAyMy0wMS0yNVQwODozNTo1OS4yMjBaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCJlbGVwaGFudC1hZC9maWxlcyJdXX0=',
    signature: '96MK/pgro8TGwTulrX3Y28CHcUU=',
    path: 'elephant-ad/files/temp/'
  }
  let serverUrl = ossConf.reqAddr
  let OSSAccessKeyId = ossConf.OSSAccessKeyId
  let policy = ossConf.policy
  let signature = ossConf.signature
  let key = ossConf.path + filename
  console.log('upLoadfile key------>' + key)

  let xhr = new XMLHttpRequest()
  xhr.open('post', serverUrl)
  xhr.upload.onprogress = function (e) {
    // 进度(e.loaded / e.total * 100)
    console.log('xhr onprogress---------->' + (e.loaded / e.total * 100))
  }
  xhr.onerror = function () {
    // 根据自己的需要添加代码
    console.log('xhr onerror---------->' + xhr.status)
    return false
  }
  xhr.onload = function (e) {
    console.log('xhr onload---------->' + xhr.status)
  }
  let formData = new FormData()
  formData.append('OSSAccessKeyId', OSSAccessKeyId) // Bucket 拥有者的Access Key Id。
  formData.append('policy', policy) // policy规定了请求的表单域的合法性
  formData.append('Signature', signature) // 根据Access Key Secret和policy计算的签名信息，OSS验证该签名信息从而验证该Post请求的合法性
  formData.append('key', key) // 文件名字，可设置路径
  formData.append('success_action_status', '200') // 让服务端返回200,不然，默认会返回204
  formData.append('file', file)
  xhr.send(formData)

  let myDomain = 'https://ad.mirrorego.com/'
  let fileUrl = myDomain + key
  console.log('upLoadfile key------>' + fileUrl)
  return fileUrl
}

uni.$upLoadfile = upLoadfile;